//
// Created by Scave on 2025/8/22.
//

#include "Resource.h"
#include "bgfx/bgfx.h"
#include "util/bgfx/BgfxUtil.h"

NS_RIDER_BEGIN
    Texture2D::Texture2D() {
    }

    Texture2D::Texture2D(TEXTURE_ID_TYPE id) : id_(id) {
    }

    bool Texture2D::IsValid() {
        return GetId() != kInvalidIndex;
    }

    TEXTURE_ID_TYPE Texture2D::GetId() const {
        return id_;
    }

    Texture2D ResourceManager::CreateOrGetTexture(const String& path) {
        if (path_texture_map_.find(path) == path_texture_map_.end()) {
            const char* str = path.c_str();
            UInt64 flags = BGFX_TEXTURE_NONE
                           | BGFX_SAMPLER_U_CLAMP
                           | BGFX_SAMPLER_V_CLAMP
                           | BGFX_SAMPLER_MIN_POINT
                           | BGFX_SAMPLER_MIP_POINT;
            bgfx::TextureHandle handle = loadTexture(str, flags);
            Texture2D result(handle.idx);
            path_texture_map_[path] = result;
            return result;
        } else {
            return path_texture_map_[path];
        }
    }

    Material ResourceManager::CreateColorMaterial(rider::Color color) {
        return {color};
    }

    Material ResourceManager::CreateTextureMaterial(const String& path) {
        return {{}, CreateOrGetTexture(path)};
    }

NS_RIDER_END
